package czy.demo.config.shiro;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.biz.realm.AuthorizingRealmListener;
import org.apache.shiro.realm.AuthorizingRealm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/* Realm监听器，由AbstractAuthorizingRealm调用
 * 当获取认证信息失败、成功时调用方法
 */
@Component
public class DefaultAuthorizingRealmListener implements AuthorizingRealmListener {

    private static final Logger logger = LoggerFactory.getLogger(DefaultAuthorizingRealmListener.class);

    @Override
    public void onFailure(AuthorizingRealm realm, AuthenticationToken token, AuthenticationException ex) {
        logger.error("用户"+token.getPrincipal()+"认证失败",ex);
    }

    @Override
    public void onSuccess(AuthorizingRealm realm, AuthenticationInfo info) {
        logger.debug("用户"+info.getPrincipals().getPrimaryPrincipal()+"认证成功");
    }
}
